﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using DTO;
using BUS;

namespace DoAnHuongRung
{
    public partial class frmLoaiMonAn : Form
    {
        public frmLoaiMonAn()
        {
            InitializeComponent();
        }

        private void frmLoaiMonAn_Load(object sender, EventArgs e)
        {
            LoadDanhSach();
        }

        private void LoadDanhSach()
        {
            dgvDanhSach.AutoGenerateColumns = false;
            dgvDanhSach.DataSource = LoaiMonAnBUS.SelectAll();

            txtMaLoai.Text = "";
            txtTenLoai.Text = "";
        }
        private void SelectData(string filterExpression)
        {
            DataTable pData = ((DataTable)dgvDanhSach.DataSource);
            DataRow[] rows = pData.Select(filterExpression);

            pData = ((DataTable)dgvDanhSach.DataSource).Clone();
            for (int i = 0; i < rows.Length; i++)
            {
                DataRow row = pData.NewRow();
                row[0] = rows[i].ItemArray[0].ToString();
                row[1] = rows[i].ItemArray[1].ToString();
                pData.Rows.Add(row);
            }
            dgvDanhSach.DataSource = pData;
        }

        private void txtTim_Enter(object sender, EventArgs e)
        {
            txtTim.Text = "";
            txtTim.ForeColor = Color.Black;
        }
        private void txtTim_Leave(object sender, EventArgs e)
        {
            if (txtTim.Text == "")
            {
                txtTim.Text = "Tìm kiếm";
                txtTim.ForeColor = Color.Silver;
            }
        }
        private void txtTim_TextChanged(object sender, EventArgs e)
        {
            if (txtTim.Text != "") SelectData("TenLoai like '%" + txtTim.Text + "%'");
            else dgvDanhSach.DataSource = LoaiMonAnBUS.SelectAll();
        }

        private void dgvDanhSach_SelectionChanged(object sender, EventArgs e)
        {
            txtMaLoai.Text = dgvDanhSach.CurrentRow.Cells[0].Value.ToString();
            txtTenLoai.Text = dgvDanhSach.CurrentRow.Cells[1].Value.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaLoai.Text != "")
            {
                txtMaLoai.Text = "";
                txtTenLoai.Text = "";
                txtTenLoai.Focus();
            }
            else if (txtTenLoai.Text == "")
            {
                MessageBox.Show("Nhập tên loại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtTenLoai.Focus();
            }
            else
            {
                //Thêm
                bool b = LoaiMonAnBUS.Insert(new LoaiMonAnDTO(0, txtTenLoai.Text));
                if (b == true)
                {
                    LoadDanhSach();
                    new SoundPlayer(Properties.Resources.Windows_Notify).Play();
                    MessageBox.Show("Đã thêm !!!");
                }
            }
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaLoai.Text == "")
                MessageBox.Show("Chọn loại món ăn cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else if (txtTenLoai.Text == "")
            {
                MessageBox.Show("Nhập tên loại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtTenLoai.Focus();
            }
            else
            {
                //Sửa
                bool b = LoaiMonAnBUS.Update(new LoaiMonAnDTO(Convert.ToInt32(txtMaLoai.Text), txtTenLoai.Text));
                if (b == true)
                {
                    LoadDanhSach();
                    new SoundPlayer(Properties.Resources.Windows_Notify).Play();
                    MessageBox.Show("Đã sửa !!!");
                }
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaLoai.Text == "")
                MessageBox.Show("Chọn loại món ăn cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else
            {
                //Xóa
                bool b = LoaiMonAnBUS.Delete(Convert.ToInt32(txtMaLoai.Text));
                if (b == true)
                {
                    LoadDanhSach();
                    new SoundPlayer(Properties.Resources.Windows_Notify).Play();
                    MessageBox.Show("Đã xóa !!!");
                }
            }
        }
    }
}
